<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <title>天知道</title>
  <link rel="stylesheet" href="css/reset.css" />
  <link rel="stylesheet" href="css/index.css" />
  <style>
    .fade-enter-active,
    .fade-leave-active {
      transition: opacity 0.5s;
    }

    .fade-enter,
    .fade-leave-to

    /* .fade-leave-active below version 2.1.8 */
      {
      opacity: 0;
    }
  </style>
</head>

<body>
  <div class="wrap" id="app">
    <div class="search_form">
      <div class="logo"><img src="img/logo.png" alt="logo" /></div>
      <div class="form_group">
        <!-- 搜索框 -->
        <input type="text" v-model="city" @keyup.enter.trim='search' class="input_txt" placeholder="请输入查询的天气" />
        <!-- 搜索按钮 -->
        <button class="input_sub" @click='search'>搜 索</button>
      </div>
      <!-- 热门城市 -->
      <div class="hotkey">
        <!-- <a href="javascript:;">北京</a><a href="javascript:;">上海</a><a href="javascript:;">广州</a><a
          href="javascript:;">深圳</a> -->
        <a href="javascript:;" v-for="(item,index) in hotCitys" @click='clickSearch(item)'>{{item}}</a>
      </div>
    </div>
    <!-- 搜索结果 -->
    <ul class="weather_list">
      <!-- 列表项 -->
      <!-- <li>
        <div class="info_type"><span class="iconfont">小雨</span></div>
        <div class="info_temp">
          <b>低温 4℃</b>
          ~
          <b>高温 13℃</b>
        </div>
        <div class="info_date"><span>3日星期一</span></div>
      </li> -->
      <li v-for="(item,index) in forecastList" :key="item.date">
        <div class="info_type">
          <span class="iconfont">{{ item.type }}</span>
        </div>
        <div class="info_temp">
          <b>{{ item.low}}</b>
          ~
          <b>{{ item.high}}</b>
        </div>
        <div class="info_date">
          <span>{{ item.date }}</span>
        </div>
      </li>
    </ul>
    <!-- 遮罩层 -->
    <transition name="fade">
      <div class="cover" v-show="isShow">
        <img src="./img/loading.gif" alt="" />
      </div>
    </transition>
  </div>
  <!-- 开发环境版本，包含了有帮助的命令行警告 -->
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  <!-- 官网提供的 axios 在线地址 -->
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <script>
    let app = new Vue({
      el: '#app',
      data: {
        city: '深圳',
        forecastList: [],
        hotCitys: ['北京', '上海', '广州', '深圳', '杭州', '大连'],
        isShow: false
      },
      methods: {
        search() {
          // 开启动画
          this.isShow = true;
          this.forecastList = [];
          // axios.get(`http://wthrcdn.etouch.cn/weather_mini?city=${this.city}`)
          //   .then((res) => {
          //     console.log(res)
          //     this.forecastList = res.data.data.forecast
          //   })
          //   .catch((err) => {
          //     console.log(err)
          //   })
          axios({
              // 注意属性值不要写错
              url: 'http://wthrcdn.etouch.cn/weather_mini',
              method: 'get',
              params: {
                city: this.city
              }
            }).then((res) => {
              console.log(res)
              setTimeout(() => {
                this.forecastList = res.data.data.forecast
                this.isShow = false;
              }, 400)
            })
            .catch((err) => {
              console.log(err)
            })
        },
        clickSearch(city) {
          this.city = city
          this.search()
        },
      },
      created() {
        this.search();
      }
    })
  </script>
</body>

</html>